﻿using IntelligentEscalator.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace IntelligentEscalator.Tools
{
    public class LogHelper
    {
        static string path = AppDomain.CurrentDomain.BaseDirectory;
        static object loglocker = new object();
        static object loglocker2 = new object();
        static object loglocker3 = new object();
        // 保存数据到本地
        public static void LogToTxt(byte ch_index, double[] ch_data,int fs,uint len,string device_sn)
        {
            lock (loglocker)
            {
                try
                {
                    string dirpath = path + @"/Log/" + @"/" + device_sn + @"/"+ DateTime.Now.ToString("yyyy-MM-dd") + @"/" + ch_index;
                    string filepath = dirpath + @"/" + DateTime.Now.Ticks + $"_{fs}_{len}.txt";
                    //生成目录
                    //创建文件夹
                    if (Directory.Exists(dirpath) == false)//如果不存在就创建file文件夹
                    {
                        Directory.CreateDirectory(dirpath);
                    }

                    // 判断文件是否存在，不存在则创建，否则读取值显示到txt文档
                    if (!System.IO.File.Exists(filepath))
                    {
                        FileStream fs1 = new FileStream(filepath, FileMode.Create, FileAccess.Write);//创建写入文件 
                        fs1.Close();
                    }
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < ch_data.Length; i++)
                    {
                        sb.Append(string.Join(",", ch_data[i]) + "\n");
                    }
                    File.AppendAllText(filepath, sb.ToString());
                }
                catch (Exception ex) { }
            }
        }
        public static void Error(string message)
        {
            try {
                lock (loglocker2)
                {
                    string dirpath = path + @"/Error";
                    string filepath = dirpath + @"/" + DateTime.Now.Ticks + ".txt";


                    //生成目录
                    //创建文件夹
                    if (Directory.Exists(dirpath) == false)//如果不存在就创建file文件夹
                    {
                        Directory.CreateDirectory(dirpath);
                    }

                    // 判断文件是否存在，不存在则创建，否则读取值显示到txt文档
                    if (!System.IO.File.Exists(filepath))
                    {
                        FileStream fs1 = new FileStream(filepath, FileMode.Create, FileAccess.Write);//创建写入文件 
                        fs1.Close();
                    }

                    File.AppendAllTextAsync(filepath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + message + "\r\n");
                }
            }
            catch (Exception e) { Console.WriteLine(e.Message); }
        }

        public static List<double> loadArrayFromTxt(string filename)
        {
            string[] lines = File.ReadAllLines(filename);
            List<double> lists = new List<double>();
            foreach (string line in lines)
            {
                if (!string.IsNullOrWhiteSpace(line))
                {
                    lists.Add(Convert.ToDouble(line.Trim()));
                }
            }
            return lists;
        }

        // 保存特征值到本地文件
        public static void LogToTxtFeature(byte ch_index, FeatureModel featureModel, int fs, uint len)
        {
            lock (loglocker3)
            {
                try
                {
                    string dirpath = path + @"/feature" +@"/"+ ch_index;
                    string filepath = dirpath + @"/" + $"{DateTime.Now.ToString("yyyy-MM")}.txt";
                    //生成目录
                    //创建文件夹
                    if (Directory.Exists(dirpath) == false)//如果不存在就创建file文件夹
                    {
                        Directory.CreateDirectory(dirpath);
                    }

                    // 判断文件是否存在，不存在则创建，否则读取值显示到txt文档
                    if (!System.IO.File.Exists(filepath))
                    {
                        FileStream fs1 = new FileStream(filepath, FileMode.Create, FileAccess.Write);//创建写入文件 
                        fs1.Close();
                    }
                    StringBuilder sb = new StringBuilder();
                    sb.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+ "#");
                    sb.Append($"{featureModel.acc_peakToPeak},");
                    sb.Append($"{featureModel.acc_peak},");
                    sb.Append($"{featureModel.acc_meanAbsolute},");
                    sb.Append($"{featureModel.acc_rmsValue},");
                    sb.Append($"{featureModel.acc_kurtosisValue},");
                    sb.Append($"{featureModel.acc_Wave},");
                    sb.Append($"{featureModel.acc_peakFactor},");
                    sb.Append($"{featureModel.acc_PulseFactor},");
                    sb.Append($"{featureModel.acc_CrestFactor},");
                    sb.Append($"{featureModel.acc_kurtosisFactor}");
                    File.AppendAllText(filepath, sb.ToString()+"\r\n");

                }
                catch (Exception ex) { }
            }
        }
    }

}
